package com.txc.pay.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * @ClassName: AliPayConfig
 * @Description: 支付宝支付配置类
 * @Author: TXC
 * @Date: 2023-12-25 11:13
 **/
@EqualsAndHashCode(callSuper = true)
@Data
public class AliPayConfig extends PayConfig implements Serializable {
    private String protocol;
    private String gatewayHost;
    private String signType;
    private String aliPayPublicKey;
    private String notifyUrl;
    private String encryptKey;

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }


    public static AliPayConfig jsonToBean(String json) {
        if (StrUtil.isNotEmpty(json)) {
            try {
                AliPayConfig aliPayConfig = JSONUtil.toBean(json, AliPayConfig.class);
                if (aliPayConfig.verifyParam()) {
                    return aliPayConfig;
                } else {
                    throw new IllegalArgumentException("AliPayConfig verification failed: " + aliPayConfig);
                }
            }catch (Exception e) {
                throw new IllegalArgumentException("Failed to parse JSON to AliPayConfig", e);
            }

        } else {
            throw new IllegalArgumentException("支付宝支付配置信息不能为空");
        }
    }

    @Override
    public boolean verifyParam() {
        super.verifyParam();
        return this.protocol != null && this.gatewayHost != null && this.signType != null
                && this.aliPayPublicKey != null
                && this.encryptKey != null;
    }
}
